package com.hdoit.kylin.logical.group.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.tennetcn.common.aop.CommonApplicationContextUtil;
import com.tennetcn.common.dao.base.impl.SuperService;
import com.hdoit.kylin.logical.baseInfo.model.Academy;
import com.hdoit.kylin.logical.baseInfo.model.School;
import com.hdoit.kylin.logical.baseInfo.service.IAcademyService;
import com.hdoit.kylin.logical.baseInfo.service.ISchoolService;
import com.hdoit.kylin.logical.group.dao.IGroupDao;
import com.hdoit.kylin.logical.group.dao.IProjectDao;
import com.hdoit.kylin.logical.group.model.Group;
import com.tennetcn.common.enums.ModelStatus;
import com.tennetcn.common.logical.superbase.model.Role;
import com.tennetcn.common.logical.superbase.service.IEmployeeService;
import com.tennetcn.common.logical.superbase.service.IRoleService;
import com.tennetcn.common.message.DaoBaseRuntimeException;
import com.tennetcn.common.message.PagerModel;
import com.tennetcn.common.message.SearchModel;
import com.tennetcn.common.utils.ChineseUtils;
import com.tennetcn.common.utils.CommonUtils;
import com.tennetcn.common.utils.StringHelper;
import com.tennetcn.plugins.devshorts.enumerable.Enumerable;
import com.hdoit.kylin.logical.group.service.IGroupService;
import com.hdoit.kylin.logical.user.model.Student;
import com.hdoit.kylin.logical.user.model.Teacher;
import com.hdoit.kylin.logical.user.service.IStudentService;
import com.hdoit.kylin.logical.user.service.ITeacherService;
import com.hdoit.kylin.logical.viewmodel.GroupSearch;
/**
* @author luozhicheng
* @email
* @createtime 2017年07月18日 20:45:17
* @comment
*/
@Component
public class GroupServiceImpl extends SuperService<Group> implements IGroupService{

	IGroupDao groupDao;
	
	
	public IGroupDao getGroupDao() {
		return groupDao;
	}

	@Resource
	public void setGroupDao(IGroupDao groupDao) {
		this.groupDao = groupDao;
	}


	@Override
	public List<Group> queryList(GroupSearch search, PagerModel pagerModel) {
		
		return groupDao.queryList(search,pagerModel);
	}
	
	@Override
	public boolean applyChange(Group group){
		ITeacherService teacherService = CommonApplicationContextUtil.getCurrentContext().getBean(ITeacherService.class);
		if(group.getModelStatus().equals(ModelStatus.add)){
			Teacher teacher = teacherService.queryModel(group.getDutyId());
			teacher.setGroupId(group.getGroupId());
			teacher.setModelStatus(ModelStatus.update);
			if(!teacherService.applyChange(teacher)){
				throw new DaoBaseRuntimeException("teacher groupId roll back");
			}
		}
	
		if(!super.applyChange(group)){
			throw new DaoBaseRuntimeException("group roll back");
		}
		return true;
	}
//	private ITeacherService teacherService;
//	private IStudentService studentService;
//	private ISchoolService schoolService;
//	private IAcademyService academyService;
//	/*by tzl*/
//	private IProjectDao projectDao;
//	private IRoleService roleService;
//	private IEmployeeService employeeService;
//	
//	public IProjectDao getProjectDao() {
//		return projectDao;
//	}
//
//	@Resource
//	public void setProjectDao(IProjectDao projectDao) {
//		this.projectDao = projectDao;
//	}
//	
//	public IEmployeeService getEmployeeService() {
//		return employeeService;
//	}
//	@Resource
//	public void setEmployeeService(IEmployeeService employeeService) {
//		this.employeeService = employeeService;
//	}
//	public IRoleService getRoleService() {
//		return roleService;
//	}
//	@Resource
//	public void setRoleService(IRoleService roleService) {
//		this.roleService = roleService;
//	}
//
//	public ITeacherService getTeacherService() {
//		return teacherService;
//	}
//
//	@Resource
//	public void setTeacherService(ITeacherService teacherService) {
//		this.teacherService = teacherService;
//	}
//
//	public IStudentService getStudentService() {
//		return studentService;
//	}
//
//	@Resource
//	public void setStudentService(IStudentService studentService) {
//		this.studentService = studentService;
//	}
//
//	public ISchoolService getSchoolService() {
//		return schoolService;
//	}
//
//	@Resource
//	public void setSchoolService(ISchoolService schoolService) {
//		this.schoolService = schoolService;
//	}
//
//	public IAcademyService getAcademyService() {
//		return academyService;
//	}
//
//	@Resource
//	public void setAcademyService(IAcademyService academyService) {
//		this.academyService = academyService;
//	}
//
//	@Override
//	public Group queryModel(String key) {
//		Group group=super.queryModel(key);
//		
//		if(null==group)
//			return null;
//		
//		Academy academy=academyService.queryModel(group.getAcademyId());
//		School school=schoolService.queryModel(academy.getSchoolId());
//		group.setSchoolName(school.getSchoolName());
//		group.setAcademyName(academy.getAcademyName());
//
//		/*by tzl*/
//		group.setProjectsName(projectDao.queryProjectNameListByGroupId(group.getGroupId()));
//		
//		return group;
//	}
//	
//	@Override
//	public boolean applyChange(Group group) {
//		if(super.applyChange(group)){
//			if(ModelStatus.add.equals(group.getModelStatus())){
//				Teacher teacher=new Teacher();
//				teacher.setEmployeeId(CommonUtils.getKeyId());
//				teacher.setEmployeeName(group.getDutyAccount());
//				teacher.setEmployeeAccount(group.getDutyAccount());
//				teacher.setEmployeeCode(ChineseUtils.getPingYin(teacher.getEmployeeName()));
//				teacher.setMarkCode(ChineseUtils.getPinYinHeadChar(teacher.getEmployeeName()));
//				teacher.setCreateDate(group.getCreateDate());
//				teacher.setTeacherName(group.getDutyName());
//				teacher.setCreateUserId(group.getCreateId());
//				teacher.setGroupId(group.getGroupId());
//				teacher.setModelStatus(ModelStatus.add);
//				
//				List<Role> roles=roleService.queryListByMark("groupDuty");
//				List<String> roleIdList=Enumerable.init(roles).map(role->role.getRoleId()).toList();
//				
//				if(!employeeService.applyRoleIds(teacher, StringHelper.join(roleIdList))){
//					throw new DaoBaseRuntimeException("applyRoleIds roll back");
//				}
//				
//				
//				if(!teacherService.applyChange(teacher)){
//					throw new DaoBaseRuntimeException("applyChange roll back");
//				}
//			}
//			return true;
//		}
//		return false;
//	}
//	
//	@Override
//	public List<Group> queryList(SearchModel search) {
//		List<Group> groups=super.queryList(search);
//		
//		for(Group group:groups){
//			Academy academy=academyService.queryModel(group.getAcademyId());
//			School school=schoolService.queryModel(academy.getSchoolId());
//			group.setSchoolName(school.getSchoolName());
//			group.setAcademyName(academy.getAcademyName());
//		}
//		
//		return groups;
//	}
//
//	@Override
//	public Group queryModelByLoginId(String loginId) {
//		Group group;
//		
//		Teacher teacher=teacherService.queryModel(loginId);
//		if(null!=teacher){
//			group=queryModel(teacher.getGroupId());
//			return group;
//		}
//		Student student=studentService.queryModel(loginId);
//		if(null!=student){
//			group=queryModel(student.getGroupId());
//			return group;
//		}
//		
//		return null;
//	}
	
	
}
